---
date: "`r Sys.Date()`"
output: pdf_document
---

<style type="text/css">
  body{
  font-family: Arial;
  font-size: 12pt;
}
</style>

```{r setup, include=FALSE}

knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, comment = "", fig.height = 6, fig.width = 10, 
                      fig.align = 'center')

# Load libraries
library(tidyverse)
library(readxl)
library(janitor)
library(extrafont)

font_import()

```

```{r data}

# read in data

df_hbox <- read_csv("Data/Hans_datatable_exports/malawi key data v04Jan2022.csv") %>% 
  as_tibble() %>% 
  janitor::clean_names() %>% 
  mutate(status = replace(status, status == "HV", "HC")) %>% 
  dplyr::rename("subject_id" = "subject_id_session") %>% 
  
  dplyr::filter(status %in% c("CM", "HC", "UM") & session_no == 1 | subject_id == "TM0003CM01") %>% 
  dplyr::filter(!grepl("blood", subject_id)) %>% 
  mutate(status = factor(status, levels = c("HC", "UM", "CM"))) %>% 
  mutate_at(c("bp_dias", "bp_sys", "glucose", "lactate"), as.numeric) %>% # converts remaining character variables to numerics
  select(subject_id, status, everything()) %>% 
  group_by(status)

```

### Fig 1. Hb_oxy and Hb_tot in the brain and muscle
```{r fig1}

# extract hb_tot and hb_oxy for the brain and muscle
df_fig1 <- df_hbox %>% 
  select(contains(c("hb_tot","hb_oxy")), -contains(c("alpha", "bsl"))) %>%
  ungroup() %>% 
  pivot_longer(2:ncol(.), names_to = "variable", values_to = "value") %>% 
  separate(variable, into = c("tissue", "variable"), sep = "_", extra = "merge") %>% 
  mutate(variable = factor(variable, levels = c("hb_tot", "hb_oxy")))

# create color vector
my_col <- c("CM" = "#00BFC4", "HC" = "#7CAE00", "UM" = "#F8766D")

# plot
df_fig1 %>% 
  
  ggplot(aes(x = status, y = value)) +
  geom_violin(aes(color = status)) +
  geom_jitter(position = position_jitter(0.2), shape = 1, size = 2) +
  stat_summary(fun = "median", geom = "crossbar", aes(color = status), size = 0.2, width = 0.5) +
  facet_grid(variable ~ tissue, scales = "free_y") +
  
  scale_color_manual(values = my_col) +
  labs(x = "") +
  theme_bw() +
  theme(legend.position = "none")  +
  
  theme(# text = element_text(family = "Arial"),
        strip.text = element_text(size = 15),
        axis.title = element_text(size = 15),
        axis.title.y = element_blank(),
        axis.text = element_text(size = 15))

```

### Fig 3. Hb_oxy alpha and Hb_tot alpha in the brain and muscle
```{r fig3}

# extract hb_tot and hb_oxy for the brain and muscle
df_fig3 <- df_hbox %>% 
  select(contains("alpha2")) %>%
  ungroup() %>% 
  pivot_longer(2:ncol(.), names_to = "variable", values_to = "value") %>% 
  separate(variable, into = c("tissue", "variable"), sep = "_", extra = "merge") %>% 
  mutate(variable = str_remove(variable, "2")) %>% 
  mutate(variable = factor(variable, levels = c("hb_tot_alpha", "hb_oxy_alpha")))

# plot
df_fig3 %>% 
  
  ggplot(aes(x = status, y = value)) +
  geom_violin(aes(color = status), fill = "white") +
  geom_jitter(position = position_jitter(0.2), shape = 1, size = 2) +
  stat_summary(fun = "median", geom = "crossbar", aes(color = status), size = 0.2, width = 0.5) +
  facet_grid(variable ~ tissue, scales = "free_y") +

  scale_color_manual(values = my_col) +
  ylim(c(0, 1.5)) +
  labs(x = "") +
  theme_bw() +
  theme(legend.position = "none")  +
  
  theme(# text = element_text(family = "Arial"),
        strip.text = element_text(size = 15),
        axis.title = element_text(size = 15),
        axis.title.y = element_blank(),
        axis.text = element_text(size = 15))

```

